<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
    <link rel="icon" href="./shop.ico" type="image/x-icon">

    <style>
        body {
            background: #eee;
            margin: 0;
            padding: 0;
            overflow-x: hidden;
        }

        .clearfix {
            content: "";
            display: table;
            clear: both;
        }

        #site-header,
        #site-footer {
            background: #fff;
        }

        #site-header {
            margin: 0 0 30px 0;
        }

        #site-header h1 {
            font-size: 31px;
            font-weight: 300;
            padding: 40px 0;
            position: relative;
            margin: 0;
        }

        a {
            color: #000;
            text-decoration: none;

            -webkit-transition: color .2s linear;
            -moz-transition: color .2s linear;
            -ms-transition: color .2s linear;
            -o-transition: color .2s linear;
            transition: color .2s linear;
        }

        a:hover {
            color: #53b5aa;
        }

        #site-header h1 span {
            color: #53b5aa;
        }

        #site-header h1 span.last-span {
            background: #fff;
            padding-right: 150px;
            position: absolute;
            left: 217px;

            -webkit-transition: all .2s linear;
            -moz-transition: all .2s linear;
            -ms-transition: all .2s linear;
            -o-transition: all .2s linear;
            transition: all .2s linear;
        }

        #site-header h1:hover span.last-span,
        #site-header h1 span.is-open {
            left: 363px;
        }

        #site-header h1 em {
            font-size: 16px;
            font-style: normal;
            vertical-align: middle;
        }

        .container {
            font-family: 'Open Sans', sans-serif;
            margin: 0 auto;
            width: 980px;
        }

        #cart {
            width: 100%;
        }

        #cart h1 {
            font-weight: 300;
        }

        #cart a {
            color: #53b5aa;
            text-decoration: none;

            -webkit-transition: color .2s linear;
            -moz-transition: color .2s linear;
            -ms-transition: color .2s linear;
            -o-transition: color .2s linear;
            transition: color .2s linear;
        }

        #cart a:hover {
            color: #000;
        }

        .product.removed {
            margin-left: 980px !important;
            opacity: 0;
        }

        .product {
            border: 1px solid #eee;
            margin: 20px 0;
            width: 100%;
            height: 195px;
            position: relative;

            -webkit-transition: margin .2s linear, opacity .2s linear;
            -moz-transition: margin .2s linear, opacity .2s linear;
            -ms-transition: margin .2s linear, opacity .2s linear;
            -o-transition: margin .2s linear, opacity .2s linear;
            transition: margin .2s linear, opacity .2s linear;
        }

        .product img {
            width: 100%;
            height: 100%;
        }

        .product header,
        .product .content {
            background-color: #fff;
            border: 1px solid #ccc;
            border-style: none none solid none;
            float: left;
        }

        .product header {
            background: #000;
            margin: 0 1% 20px 0;
            overflow: hidden;
            padding: 0;
            position: relative;
            width: 24%;
            height: 195px;
        }

        .product header:hover img {
            opacity: .7;
        }

        .product header:hover h3 {
            bottom: 73px;
        }

        .product header h3 {
            background: #53b5aa;
            color: #fff;
            font-size: 22px;
            font-weight: 300;
            line-height: 49px;
            margin: 0;
            padding: 0 30px;
            position: absolute;
            bottom: -50px;
            right: 0;
            left: 0;

            -webkit-transition: bottom .2s linear;
            -moz-transition: bottom .2s linear;
            -ms-transition: bottom .2s linear;
            -o-transition: bottom .2s linear;
            transition: bottom .2s linear;
        }

        .remove {
            cursor: pointer;
        }

        .product .content {
            box-sizing: border-box;
            -moz-box-sizing: border-box;
            height: 140px;
            padding: 0 20px;
            width: 75%;
        }

        .product h1 {
            color: #53b5aa;
            font-size: 25px;
            font-weight: 300;
            margin: 17px 0 20px 0;
        }

        .product footer.content {
            height: 50px;
            margin: 6px 0 0 0;
            padding: 0;
        }

        .product footer .price {
            background: #fcfcfc;
            color: #000;
            float: right;
            font-size: 15px;
            font-weight: 300;
            line-height: 49px;
            margin: 0;
            padding: 0 30px;
        }

        .product footer .full-price {
            background: #53b5aa;
            color: #fff;
            float: right;
            font-size: 22px;
            font-weight: 300;
            line-height: 49px;
            margin: 0;
            padding: 0 30px;

            -webkit-transition: margin .15s linear;
            -moz-transition: margin .15s linear;
            -ms-transition: margin .15s linear;
            -o-transition: margin .15s linear;
            transition: margin .15s linear;
        }

        .qt,
        .qt-plus,
        .qt-minus {
            display: block;
            float: left;
        }

        .qt {
            font-size: 19px;
            line-height: 50px;
            width: 70px;
            text-align: center;
        }

        .qt-plus,
        .qt-minus {
            background: #fcfcfc;
            border: none;
            font-size: 30px;
            font-weight: 300;
            height: 100%;
            padding: 0 20px;
            -webkit-transition: background .2s linear;
            -moz-transition: background .2s linear;
            -ms-transition: background .2s linear;
            -o-transition: background .2s linear;
            transition: background .2s linear;
        }

        .qt-plus:hover,
        .qt-minus:hover {
            background: #53b5aa;
            color: #fff;
            cursor: pointer;
        }

        .qt-plus {
            line-height: 50px;
        }

        .qt-minus {
            line-height: 47px;
        }

        #site-footer {
            margin: 30px 0 0 0;
        }

        #site-footer {
            padding: 40px;
        }

        #site-footer h1 {
            background: #fcfcfc;
            border: 1px solid #ccc;
            border-style: none none solid none;
            font-size: 24px;
            font-weight: 300;
            margin: 0 0 7px 0;
            padding: 14px 40px;
            text-align: center;
        }

        #site-footer h2 {
            font-size: 24px;
            font-weight: 300;
            margin: 10px 0 0 0;
        }

        #site-footer h3 {
            font-size: 19px;
            font-weight: 300;
            margin: 15px 0;
        }

        .left {
            float: left;
        }

        .right {
            float: right;
        }

        .btn {
            background: #53b5aa;
            border: 1px solid #999;
            border-style: none none solid none;
            cursor: pointer;
            display: block;
            color: #fff;
            font-size: 20px;
            font-weight: 300;
            padding: 16px 0;
            width: 290px;
            text-align: center;

            -webkit-transition: all .2s linear;
            -moz-transition: all .2s linear;
            -ms-transition: all .2s linear;
            -o-transition: all .2s linear;
            transition: all .2s linear;
        }

        .btn:hover {
            color: #fff;
            background: #429188;
        }

        .type {
            background: #fcfcfc;
            font-size: 13px;
            padding: 10px 16px;
            left: 100%;
        }

        .type,
        .color {
            border: 1px solid #ccc;
            border-style: none none solid none;
            position: absolute;
        }

        .color {
            width: 40px;
            height: 40px;
            right: -40px;
        }

        .red {
            background: #cb5a5e;
        }

        .yellow {
            background: #f1c40f;
        }

        .blue {
            background: #3598dc;
        }

        .minused {
            margin: 0 50px 0 0 !important;
        }

        .added {
            margin: 0 -50px 0 0 !important;
        }


        .header{

            background-color: rgb(255, 255, 255);
            position: relative;
            padding: 5px 0;
        }

        .header-text{
            font-size: 30px;
            text-align: center;
        }
        


        .header-btn {
            background: #53b5aa;
            border: 1px solid #999;
            border-style: none none solid none;
            cursor: pointer;
            display: block;
            color: #fff;
            font-size: 20px;
            font-weight: 300;
            padding: 16px 0;
            width: 290px;
            text-align: center;

            -webkit-transition: all .2s linear;
            -moz-transition: all .2s linear;
            -ms-transition: all .2s linear;
            -o-transition: all .2s linear;
            transition: all .2s linear;
        }

        .header-btn:hover {
            color: #fff;
            background: #429188;
        }

        .header-btn{
            position: absolute;
            right: 0;
            top: 0;
            bottom: 0;
            height: auto;
            padding: 0;
            font-size: 20px;
            line-height: 50px;
        }


        .loading-block {
            background: rgba(0, 0, 0, 0.2);
            width: 100%;
            height: 100%;
            position: fixed;
            top: 0;
            bottom: 0;
            z-index: 10;
            display: flex;
            flex-direction: column;
            align-items: center;
        }

        @keyframes rotate-loading {
            0% {
                transform: rotate(0deg);
                -ms-transform: rotate(0deg);
                -webkit-transform: rotate(0deg);
                -o-transform: rotate(0deg);
                -moz-transform: rotate(0deg);
            }

            100% {
                transform: rotate(360deg);
                -ms-transform: rotate(360deg);
                -webkit-transform: rotate(360deg);
                -o-transform: rotate(360deg);
                -moz-transform: rotate(360deg);
            }
        }


        @keyframes loading-text-opacity {
            0% {
                opacity: 0
            }

            20% {
                opacity: 0
            }

            50% {
                opacity: 1
            }

            100% {
                opacity: 0
            }
        }


        .loading-container,
        .loading {
            height: 100px;
            position: relative;
            width: 100px;
            border-radius: 100%;
        }


        .loading-container {
            margin: 40px auto
        }

        .loading {
            border: 2px solid transparent;
            border-color: transparent #fff transparent #FFF;
            animation: rotate-loading 1.5s linear 0s infinite normal;
            transform-origin: 50% 50%;
        }

        .loading-container:hover .loading {
            border-color: transparent #E45635 transparent #E45635;
        }

        .loading-container:hover .loading,
        .loading-container .loading {
            transition: all 0.5s ease-in-out;
        }

        #loading-text {
            animation: loading-text-opacity 2s linear 0s infinite normal;
            color: #ffffff;
            font-size: 10px;
            font-weight: bold;
            margin-top: 45px;
            opacity: 0;
            position: absolute;
            text-align: center;
            text-transform: uppercase;
            top: 0;
            width: 100px;
        }

        .hide {
            display: none;
        }

        .tip-text {
            font-size: 2em;
        }
    </style>

</head>

<body>

    <header>
        <div class="header">
            <div class="header-text">id:123456,用户xxx的购物车 </div>
            <div class="header-btn">返回主页面</div>

        </div>

    </header>


    <div class="container">

        <section id="cart">

        </section>

    </div>

    <footer id="site-footer">
        <div class="container clearfix">

            <div class="right">
                <h1 class="total">总价: <span>0</span>元</h1>
                <a class="btn">结算</a>
            </div>

        </div>
    </footer>

    <div class="loading-block hide">
        <div class="loading-container">
            <div class="loading"></div>
            <div id="loading-text">loading</div>
        </div>

        <!--// Link Attribution -->
        <div class="tip-text">订单处理中...</div>
    </div>



    <script>

        var check = false;
        let data=[];

        $(".header-btn").click(function () {
            location.href = "index.html";
        });

        function changeVal(el) {
            var qt = parseFloat(el.parent().children(".qt").html());
            var price = parseFloat(el.parent().children(".price").html());
            var eq = Math.round(price * qt * 100) / 100;
            el.parent().children(".full-price").html(eq + "元");
            changeTotal();
        }

        function changeTotal() {

            var price = 0;

            $(".full-price").each(function (index) {
                price += parseFloat($(".full-price").eq(index).html());
            });

            price = Math.round(price * 100) / 100;
            var fullPrice = price;

            if (price == 0) {
                fullPrice = 0;
            }

            $(".total span").html(fullPrice);
        }

        $(document).ready(function () {

            $.ajax({
                    url: "/cart/get",
                    type: "GET",
                    dataType: "json",
                    success: function(json) {
                        if (json.state == 200) {
                            console.log(json.data);
                            data=json.data;
                            if(data.length==0){
                                check=true;
                            }
                            for (let i = 0; i < data.length; i++){
                                let item="            <article class=\"product\">\n" +
                                    "                <header>\n" +
                                    "                    <a class=\"remove\">\n" +
                                    "                        <img src=\""+data[i].image+ "\" alt=\"\">\n" +
                                    "\n" +
                                    "                        <h3>移除该商品</h3>\n" +
                                    "                    </a>\n" +
                                    "                </header>\n" +
                                    "\n" +
                                    "                <div class=\"content\">\n" +
                                    "\n" +
                                    "                    <h1>"+data[i].name+"</h1>\n" +
                                    "\n" +
                                    data[i].description +
                                    "\n" +
                                    "                </div>\n" +
                                    "\n" +
                                    "                <footer class=\"content\">\n" +
                                    "\n" +
                                    "                    <span class=\"qt-minus\">-</span>\n" +
                                    "                    <span class=\"qt\">"+data[i].num+"</span>\n" +
                                    "                    <span class=\"qt-plus\">+</span>\n" +
                                    "\n" +
                                    "                    <h2 class=\"full-price\">\n" +
                                    "                        "+data[i].price*data[i].num+"元\n" +
                                    "                    </h2>\n" +
                                    "\n" +
                                    "                    <h2 class=\"price\">\n" +
                                    "                        "+data[i].price+"元\n" +
                                    "                    </h2>\n" +
                                    "                </footer>\n" +
                                    "            </article>"


                                $("#cart").append(item)


                            }

                        } else {
                            alert("获取购物车信息失败！" + json.message);
                            console.log("FALSE")
                        }
                    }
                });

            $.ajax({
                url: "/users/getsession",
                type: "POST",
                dataType: "json",
                success: function(json) {
                    if (json.state == 200) {
                        $(".header-text").html("uid:"+json.data.uid+",用户"+json.data.username+"的购物车")
                    } else {
                        alert("获取用户信息失败！" + json.message);
                        console.log("FALSE")
                    }
                }
            });

            window.setTimeout(function (){

                $(".remove").click(function () {


                    if (check == false) {
                        let that=this;
                        let index=$(".remove").index(this);
                        console.log($(".remove").index(this));

                        $.ajax({
                            url: "/cart/delete",
                            data:"cid="+data[$(".remove").index(this)].cid,
                            type: "POST",
                            dataType: "json",
                            success: function(json) {
                                if (json.state == 200) {


                                    var el = $(that);
                                    el.parent().parent().addClass("removed");
                                    window.setTimeout(
                                        function () {
                                            el.parent().parent().slideUp('fast', function () {
                                                el.parent().parent().remove();
                                                if ($(".product").length == 0) {
                                                    if (check) {
                                                        $("#cart").html("<h1>购物车为空</h1>");
                                                    } else {
                                                        $("#cart").html("<h1>购物车为空</h1>");
                                                    }
                                                }
                                                changeTotal();
                                            });
                                        }, 200);

                                    data.splice(index,1);
                                    console.log(data);


                                } else {
                                    alert("获取用户信息失败！" + json.message);
                                    console.log("FALSE")
                                }
                            }
                        });

                    }
                    else {
                        var el = $(this);
                        el.parent().parent().addClass("removed");
                        window.setTimeout(
                            function () {
                                el.parent().parent().slideUp('fast', function () {
                                    el.parent().parent().remove();
                                    if ($(".product").length == 0) {
                                        if (check) {
                                            $("#cart").html("<h1>购物车为空</h1>");
                                        } else {
                                            $("#cart").html("<h1>购物车为空</h1>");
                                        }
                                    }
                                    changeTotal();
                                });
                            }, 200);

                    }

                });


                $(".qt-plus").click(function () {
                    let that=this;

                    console.log($(".qt-plus").index(this));
                    console.log("cid="+data[$(".qt-plus").index(this)].cid);
                    $.ajax({
                        url: "/cart/add",
                        data:"cid="+data[$(".qt-plus").index(this)].cid,
                        type: "POST",
                        dataType: "json",
                        success: function(json) {
                            if (json.state == 200) {
                                $(that).parent().children(".qt").html(parseInt($(that).parent().children(".qt").html()) + 1);

                                $(that).parent().children(".full-price").addClass("added");

                                var el = $(that);
                                window.setTimeout(function () { el.parent().children(".full-price").removeClass("added"); changeVal(el); }, 150);

                            } else {
                                alert("获取用户信息失败！" + json.message);
                                console.log("FALSE")
                            }
                        }
                    });

                });

                $(".qt-minus").click(function () {

                    let that=this;

                    console.log($(".qt-minus").index(this));
                    console.log("cid="+data[$(".qt-minus").index(this)].cid);
                    $.ajax({
                        url: "/cart/minus",
                        data:"cid="+data[$(".qt-minus").index(this)].cid,
                        type: "POST",
                        dataType: "json",
                        success: function(json) {
                            if (json.state == 200) {

                                child = $(that).parent().children(".qt");

                                if (parseInt(child.html()) > 1) {
                                    child.html(parseInt(child.html()) - 1);
                                }

                                $(that).parent().children(".full-price").addClass("minused");

                                var el = $(that);
                                window.setTimeout(function () { el.parent().children(".full-price").removeClass("minused"); changeVal(el); }, 150);

                            } else {
                                alert("获取用户信息失败！" + json.message);
                                console.log("FALSE")
                            }
                        }
                    });


                });

                window.setTimeout(function () { $(".is-open").removeClass("is-open") }, 200);

                $(".btn").click(function () {
                    if(check==true){
                        alert("结算失败");
                        return;
                    }


                    $(".loading-block").removeClass("hide");
                    $.ajax({
                        url: "/cart/check",
                        type: "POST",
                        dataType: "json",
                        success: function(json) {
                            if (json.state == 200) {
                                alert("结算成功！");
                                check = true;
                                $(".remove").click();
                            } else {
                                alert("结算失败！" + json.message);
                                console.log("FALSE")
                            }
                        },
                        complete:function () {
                            $(".loading-block").addClass("hide");
                        }
                    });


                });

                changeTotal();


            },500);


        });
    </script>
</body>

</html>